<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Uc
 *
 * @author sunday 2014.05.26 add | dragon.hu 2015.6.11 modify
 */
require_once 'BaseChannel.php';

//UC平台服务器端接入
class Uc implements BaseChannel {

  //put your code here
    function __construct() {
		
    }
	
    //2015.6.11换成了守护者们游戏的apiKey，cpId和gameId
    function android($data) 
    {
    	$cpId = 52256; 
		$apiKey = '7c4e4e780f1202cfb417976a7375d946';  //守护者们
    	echo $this->payCallBackMethod($cpId, $apiKey, ANDROID);
    }

    function ios($data) 
    {
    	$cpId = 52256; 
		$apiKey = '7c4e4e780f1202cfb417976a7375d946';  //守护者们
        echo $this->payCallBackMethod($cpId, $apiKey, IOS);
    }
    
	/**
	 * 支付回调通知。
	 * 注：
	 * 1.sdk server通过http post请求cp server的接口，cp server都要有一个响应，响应内容是payCallback函数的返回值
	 * $platform:平台名字
	 */
	public function payCallBackMethod($cpId, $apiKey, $platform){
		
		$request = file_get_contents("php://input");
		//echo " request:$request \r\n";
		//var_dump($_REQUEST);
		//$request = $_REQUEST;
		$orderLog = new ChargeOrder();
		$orderLog->log("uc", $_REQUEST);
		//var_dump(json_decode($_REQUEST,true));
		try{
			//var_dump($request);
			
			$responseData = json_decode($request,true);
			
			//var_dump($responseData);
			//echo "++++++++++++<br>";
			//print_r($_REQUEST);
			//echo "<br>------------<br>";
			//echo "sign = ".$responseData['sign'];
			//var_dump($responseData['data']);
			//echo "<br>------------<br>";
			//exit(0);
			if($responseData!=null){
				/*$orderLog->log("uc",array("sign"=>$responseData['sign'],"orderId"=>$responseData['data']['orderId'],
				"gameId"=>$responseData['data']['gameId'],"serverId"=>$responseData['data']['serverId'],
				"ucid"=>$responseData['data']['ucid'],"payWay"=>$responseData['data']['payWay'],
				"amount"=>$responseData['data']['amount'],"callbackInfo"=>$responseData['data']['callbackInfo'],
				"orderStatus"=>$responseData['data']['orderStatus'],"failedDesc"=>$responseData['data']['failedDesc']));*/
				//2015.6.11修改
				$orderLog->log("uc", array("sign" => $responseData['sign'], "orderId" => $responseData['data']['orderId'], 
				"gameId" => $responseData['data']['gameId'], "accountId" => $responseData['data']['accountId'], "creator" => $responseData['data']['creator'],
				"payWay" => $responseData['data']['payWay'], "amount" => $responseData['data']['amount'], "callbackInfo" => $responseData['data']['callbackInfo'],
				"orderStatus" => $responseData['data']['orderStatus'], "failedDesc" => $responseData['data']['failedDesc'], "cpOrderId" => $responseData['data']['cpOrderId']
				));
				/*
				假定cpId=109，apiKey=202cb962234w4ers2aaa

				sign的签名规则:data的所有子参数按key升序，key=value串接即
				      MD5(cpId + amount=...+callbackInfo=...+failedDesc=...+gameId=...+orderId=...
				               +orderStatus=...+payWay=...+serverId=...+ucid=...+apiKey)（去掉+；替换...为实际值）
				签名原文：
				109amount=100callbackInfo=aaafailedDesc=gameId=123orderId=abcf1330orderStatus=SpayWay=1
				serverId=654ucid=123456202cb962234w4ers2aaa
				*/
				/*$signSource = $cpId."amount=".$responseData['data']['amount']."callbackInfo=".$responseData['data']['callbackInfo']
						."failedDesc=".$responseData['data']['failedDesc']."gameId=".$responseData['data']['gameId']
						."orderId=".$responseData['data']['orderId']."orderStatus=".$responseData['data']['orderStatus']
						."payWay=".$responseData['data']['payWay']."serverId=".$responseData['data']['serverId']
						."ucid=".$responseData['data']['ucid'].$apiKey;*/
				
				//2015.6.11修改    注意：cpOrderId 仅当回调时具备该参数时，才需加入签名，如无，则不需要加入(此处是没有cpOrderId的)
				$signSource = "accountId=".$responseData['data']['accountId']."amount=".$responseData['data']['amount']."callbackInfo=".$responseData['data']['callbackInfo'].
		/*"cpOrderId=".$responseData['data']['cpOrderId'].*/"creator=".$responseData['data']['creator']."failedDesc=".$responseData['data']['failedDesc'].
		"gameId=".$responseData['data']['gameId']."orderId=".$responseData['data']['orderId']."orderStatus=".$responseData['data']['orderStatus'].
		"payWay=".$responseData['data']['payWay'].$apiKey;  //以后切记不要把“apiKey=”加上了
						
				//echo "signSource=".$signSource;
				$sign = md5($signSource);
				//echo "<br>sign=" .$sign;
				//exit(0);
				//$orderLog->log("uc","[uc.php]"."[signSocurce]:".$signSource);
				//$orderLog->log("uc","[uc.php]"."[sign]:".$sign);
				
				//echo " sign:$sign responseData:".$responseData[sign]."\r\n";
				
				if($sign == $responseData['sign'])
				//if(1==1)	//for test
				{
					/*
					* 游戏服务器需要处理给玩家充值代码,由游戏合作商开发完成。  //这一块都是一致的
					*/
					if('S'== $responseData['data']['orderStatus'])	//支付状态：S-成功支付 F-支付失败
					{
						$status = STATUS_SUCCESS;
					}else 
					{
						$status = STATUS_FAILD;
					}
					//echo "first";
					if(false == $orderLog->recordOrder($status, $responseData['data']['amount'], $responseData['data']['callbackInfo'], "uc", $platform,$responseData['data']['orderId']))
					{
						$orderLog->log("uc","amount:".$responseData['data']['amount']." orderid:"
								.$responseData['data']['callbackInfo']." platform:".$platform." failure");
						//写入数据库失败，要返回false
						$orderLog->log("uc",  " order:".$responseData['data']['callbackInfo']." insert database failure \r\n");
						return 'FAILURE';
			        } 		
			        //echo "second";			
					$orderLog->log("uc","amount:".$responseData['data']['amount']." orderid:"
								.$responseData['data']['callbackInfo']." platform:".$platform." success");
					//echo " SUCCESS \r\n";
					//echo "success";
					return 'SUCCESS';//返回给sdk server的响应内容 
					
				}else{
					//echo " failure2 \r\n";
					$orderLog->log("uc",  " order:".$responseData['data']['callbackInfo']." sign failure \r\n");
					return 'FAILURE';//返回给sdk server的响应内容 ,对于重复多次通知失败的订单,请参考文档中通知机制。
					//echo " sign error ";
				}		
			}else{
				$orderLog->log("uc", " json_decode failure \r\n");
				//echo " interface return error \r\n";
				//return 'FAILURE';
				throw new exception(' interface return error');
				//echo "interface return error ";
			}
		}catch(Exception $e){
			$orderLog->log("uc",  " unknow error \r\n");
			//echo " exception error \r\n";
			//return 'FAILURE';
			throw new exception($e->getMessage());
			//echo "unknow error";
		}
		return 'FAILURE';//返回给sdk server的响应内容 ,对于重复多次通知失败的订单,请参考文档中通知机制。
	}
    
}
?>